﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace DataPager
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:UnorderDataPager runat=server></{0}:UnorderDataPager>")]
    public class UnorderedListDataPager : System.Web.UI.WebControls.DataPager
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        public virtual string CssClass
        {
            get
            {
                if (ViewState["CssClass"] == null)
                {
                    ViewState["CssClass"] = "pagination pagination-centered";
                }
                return (string)ViewState["CssClass"];
            }
            set
            {
                ViewState["CssClass"] = value;
            }
        }
        //protected override HtmlTextWriterTag TagKey
        //{
        //    get
        //    {
        //        return HtmlTextWriterTag.Ul;
        //    }
        //}

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            //if (HasControls())
            //{
            //    writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
            //}
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (HasControls())
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);

                foreach (Control child in Controls)
                {
                    var item = child as DataPagerFieldItem;
                    if (item == null || !item.HasControls())
                    {
                        child.RenderControl(writer);
                        continue;
                    }

                    foreach (Control ctrl in item.Controls)
                    {
                        var space = ctrl as LiteralControl;
                        if (space != null && space.Text == "&nbsp;")
                            continue;

                        // Set specific classes for li-Tag
                        if (ctrl is System.Web.UI.WebControls.WebControl)
                        {
                            // Enabled = false -> "disabled"
                            if (!((System.Web.UI.WebControls.WebControl)ctrl).Enabled)
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Class, "disabled");
                            }

                            // there can only be one Label in the datapager -> "active"
                            if (ctrl is System.Web.UI.WebControls.Label)
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Class, "active");
                            }
                        }
                        writer.RenderBeginTag(HtmlTextWriterTag.Li);
                        ctrl.RenderControl(writer);
                        writer.RenderEndTag();
                    }
                }

                writer.RenderEndTag();
            }


            // ------------------OLD ONE ----------
            //if (HasControls())
            //{
            //    foreach (Control child in Controls)
            //    {
            //        var item = child as DataPagerFieldItem;
            //        if (item == null || !item.HasControls())
            //        {
            //            child.RenderControl(writer);
            //            continue;
            //        }

            //        foreach (Control button in item.Controls)
            //        {
            //            var space = button as LiteralControl;
            //            if (space != null && space.Text == "&nbsp;") continue;

            //            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            //            button.RenderControl(writer);
            //            writer.RenderEndTag();
            //        }
            //    }


            //}
        }
    }
}